package com.example.aop;


import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yanzhao
 * @date 2022/6/2 16:35
 * @since 1.0.0
 */
public class AdvisedSupport implements Advised {

    private Object target;

    private List<Class<?>> interfaces = new ArrayList<>();

    AdvisorChainFactory advisorChainFactory = new DefaultAdvisorChainFactory();

    private List<Advisor> advisors = new ArrayList<>();

    public List<Advisor> getAdvisors() {
        return advisors;
    }

    public List<Object> getInterceptors(Method method, Class<?> targetClass) {
        return advisorChainFactory.getInterceptors(this, method, targetClass);
    }

    public Object getTarget() {
        return target;
    }

    public void setTarget(Object target) {
        this.target = target;
    }

    public void setInterfaces(Class<?>... interfaces) {
        for (Class<?> ifc : interfaces) {
            this.interfaces.add(ifc);
        }
    }

    public void addAdvice(Advice advice) {
        this.advisors.add(new DefaultPointcutAdvisor(new Pointcut() {
            @Override
            public MethodMatcher getMethodMatcher() {
                return new MethodMatcher() {
                    @Override
                    public boolean matches(Method method) {
                        return true;
                    }
                };
            }
        }, advice));
    }

    public Class<?>[] getProxiedInterfaces() {
        return ClassUtils.toClassArray(this.interfaces);
    }
}
